
import mysql.connector

mydb = mysql.connector.connect(
    host="192.168.109.100",
    user="root",
    passwd="R#21",
    charset='utf8',
    database="yt_db",
    auth_plugin='mysql_native_password'
)

mycursor = mydb.cursor()
# 创建数据库
# mycursor.execute("CREATE DATABASE yt_db")


# mycursor.execute("SHOW DATABASES")
#
# for x in mycursor:
#     print(x)
#创建表
# mycursor.execute("CREATE TABLE sites (name VARCHAR(255), url VARCHAR(255))")
#添加主键约束

# mycursor.execute("ALTER TABLE sites ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY")

#插入测试
# sql = "INSERT INTO sites (name, url) VALUES (%s, %s)"
# val = ("RUNOOB", "https://www.runoob.com")
# mycursor.execute(sql, val)
#
# mydb.commit()  # 数据表内容有更新，必须使用到该语句
#
# print(mycursor.rowcount, "记录插入成功。")

# 插入后，取得ID
# sql = "INSERT INTO sites (name, url) VALUES (%s, %s)"
# val = ("Zhihu", "https://www.zhihu.com")
# mycursor.execute(sql, val)
#
# mydb.commit()
#
# print("1 条记录已插入, ID:", mycursor.lastrowid)


sql = "UPDATE sites SET name = 'ZH' WHERE name = 'Zhihu'"

mycursor.execute(sql)

mydb.commit()

print(mycursor.rowcount, " 条记录被修改")